<template>
    <div class="goodsinfo-container">
        <transition
        @before-enter="beforeEnter"
        @enter="enter"
        @after-enter="afterEnter"
        >
            <div class="ball" v-show="flag" ref="ball">

            </div>
        </transition>

         <!-- 商品轮播图区域 -->
        <div class="mui-card">
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                <!-- <swiper :lunbotuList="lunbotu" :isfull="false"></swiper> -->
                <swiper-box :lunbotuList="lunbotuList" isfull="true"></swiper-box>
                </div>
            </div>
        </div>

        <!-- 商品购买区域 -->
         <div class="mui-card">
            <div class="mui-card-header">{{getGoodsInfo.title}}</div>
                <div class="mui-card-content">
                    <div class="mui-card-content-inner">
                        <p class="price">
                            市场价：<del>￥998</del>&nbsp;&nbsp;销售价：<span class="now_price">￥888</span>
                        </p>
                        <p>购买数量：<num-box @getSelectCount="getSelectCount" :max="10" :min="1" :step="2"></num-box></p>
                        <p>
                            <mt-button type="primary" size="small">立即购买</mt-button>
                            <mt-button type="danger" size="small" @click="flag=!flag">加入购物车</mt-button>
                            <!-- 分析： 如何实现加入购物车时候，拿到 选择的数量 -->
                            <!-- 1. 经过分析发现： 按钮属于 goodsinfo 页面， 数字 属于 numberbox 组件 -->
                            <!-- 2. 由于涉及到了父子组件的嵌套了，所以，无法直接在 goodsinfo 页面zhong 中获取到 选中的商品数量值-->
                            <!-- 3. 怎么解决这个问题：涉及到了 子组件向父组件传值了（事件调用机制） -->
                            <!-- 4. 事件调用的本质： 父向子传递方法，子调用这个方法， 同时把 数据当作参数 传递给这个方法 -->
                        </p>
                    </div>
                </div>
            </div>

        <!-- 商品参数区域 -->
        <div class="mui-card">
            <div class="mui-card-header">商品参数</div>
                <div class="mui-card-content">
                    <div class="mui-card-content-inner">
                        <p>商品货号：695919</p>
                        <p>库存情况：5件</p>
                        <p>上架时间：2018-8-1 11:11:11</p>
                    </div>
                </div>
                <div class="mui-card-footer">
                    <mt-button type="primary" size="large" @click="getDesc(id)">图文介绍</mt-button>
                    <mt-button type="danger" size="large" @click="getComment(id)">商品评论</mt-button>
                </div>
        </div>
    </div>
</template>

<script>
import Swiper from '../subcomponents/Swiper.vue'
import Numbox from '../subcomponents/GoodInfo-Num.vue'
export default {
    data: ()=>({
        flag:false,
        id:'',
        lunbotuList:[],
        getGoodsInfo:{}
    }),
    created(){
        this.id = this.$route.params.id
        this.getLunbotu()
        this.getGoodsinfo()
    },
    methods:{
        getLunbotu(){
            this.$http.get('api/getthumimages/'+this.id).then(result=>{
                // console.log(result)
                if(result.body.status === 0){
                    //循环每一项.把每一项的src改成img
                    result.body.message.forEach(item=>{
                        item.img = item.src
                    })
                    //成功了
                    this.lunbotuList = result.body.message
                    // Toast('加载轮播图成功')
                }else{
                    Toast('加载轮播图失败')
                }
            })
        },
        getGoodsinfo(){
            this.$http.get('api/goods/getdesc/'+this.id).then(result=>{
                if(result.body.status === 0){
                    this.getGoodsInfo = result.body.message
                }
            })
        },
        getDesc(id){
            this.$router.push('/home/goodsdesc/'+id)
        },
        getComment(id){
            this.$router.push({name:'goodscomment',params:{id}})
        },
        //购物车半场动画
        beforeEnter(el){
            el.style.transform="translate(0,0)"
        },
        enter(el,done){
            //小球位置
            const ballPosition = this.$refs.ball.getBoundingClientRect()
            //购物车位置
            const badgePosition = document.getElementById('badge').getBoundingClientRect()

            const xx = badgePosition.left - ballPosition.left
            const yy = badgePosition.top  - ballPosition.top

            el.offsetWidth;
            el.style.transform=`translate(${xx}px,${yy}px)`;
            el.style.transition="all 1s linear"
            done()
        },
        afterEnter(el){
            this.flag = !this.flag
        },
        // - + 
        getSelectCount(data){
            console.log(data)
        }
    },
    components:{
        'swiper-box':Swiper,
        'num-box':Numbox
    }
}
</script>

<style lang="less" scoped>
    .goodsinfo-container {
  background-color: #fff;
  overflow: hidden;

  .now_price {
    color: red;
    font-size: 16px;
    font-weight: bold;
  }

  .mui-card-footer {
    display: block;
    button {
      margin: 15px 0;
    }
  }

  .ball {
    width: 15px;
    height: 15px;
    border-radius: 50%;
    background-color: red;
    position: absolute;
    z-index: 99;
    top: 431px;
    left: 68px;
  }
}

</style>